<?php
defined('BASEPATH') OR exit('No direct script access allowed');
/**
 * 用户数据统计类
 */
class Report_Class{
	protected $ci;
	
	private $_db,$_admin_db;

	private $robot_str	= "101111,101112,101113,101114,101115,101116,101117,101118,101119,101120,101121,101122,101123,101124,101125,101126,101127,101128,101129,101130,101131,101132,101133,101134,101135,101136,101137,101138,101139,101140,101141,101142,101143,101144,101145,101146,101147,101148,101149,101150,101151,101152,101153,101154,101155,101156,101157,101158,101159,101160,101161,101162,101163,101164,101165,101166,101167,101168,101169,101170,101171,101172,101173,101174,101175,101176,101177,101178,101179,101180,101181,101182,101183,101184,101185,101186,101187,101188,101189,101190,101191,101192,101193,101194,101195,101196,101197,101198,101199,101200,101201,101202,101203,101204,101205,101206,101207,101208,101209,101210,101211";
	function __construct(){
		$this->ci = & get_instance();

		$this->_db = $this->ci->load->database('default',true);
		$this->_admin_db = $this->ci->load->database('admin',true);
	}

    public function getCfdHistoryYk($uid="",$start_date="",$end_date="",$name="",$dtype=""){
		$where	=	"where kp='close'";
		$where	.=   !empty($uid) ?" and uid ='$uid'":" and uid not in(".$this->robot_str.")";
		$where	.=   !empty($name) ?" and name ='$name'":"";
		$where	.=   !empty($start_date) ?" and date >='$start_date'":"";
		$where	.=   !empty($end_date) ?" and date <='$end_date'":"";
		$where	.=   !empty($dtype) ?" and dtype ='$dtype'":"";

		$sql	=	"select sum(yk) as history_yk from cfd_cfd_trade_log ".$where;
		$query	=	$this->_db->query($sql);
		$row	=	$query->row_array();
		if (!empty($row['history_yk'])) {
			return del0($row['history_yk']);
		}
		return	0;
    }

	public function getCfdUserNum($start_date="",$end_date="",$name="",$dtype=""){
		$where	=	"where uid not in(".$this->robot_str.")";
		$where	.=   !empty($name) ?" and name ='$name'":"";
		$where	.=   !empty($start_date) ?" and date >='$start_date'":"";
		$where	.=   !empty($end_date) ?" and date <='$end_date'":"";
		$where	.=   !empty($dtype) ?" and dtype ='$dtype'":"";

		$sql	=	"select count(distinct uid) as num from cfd_cfd_trade_log ".$where;
		$query	=	$this->_db->query($sql);
		$row	=	$query->row_array();
		if (!empty($row['num'])) {
			return $row['num'];
		}
		return	0;
    }
	
	//开仓数量
	public function getCfdKaiNum($start_date="",$end_date="",$name="",$dtype=""){
		$where	=	"where source in('10001','50001') and uid not in(".$this->robot_str.")";
		$where	.=   !empty($name) ?" and name ='$name'":"";
		$where	.=   !empty($start_date) ?" and date >='$start_date'":"";
		$where	.=   !empty($end_date) ?" and date <='$end_date'":"";
		$where	.=   !empty($dtype) ?" and dtype ='$dtype'":"";

		$sql	=	"select sum(num) as num from cfd_cfd_trade_log ".$where;
		$query	=	$this->_db->query($sql);
		$row	=	$query->row_array();
		if (!empty($row['num'])) {
			return $row['num'];
		}
		return	0;
    }

	//用户开仓数量
	public function getUserKaiNum($start_date="",$end_date="",$name="",$dtype=""){
		$where	=	"where source in('10001','50001')";
		$where	.=   !empty($name) ?" and name ='$name'":"";
		$where	.=   !empty($start_date) ?" and date >='$start_date'":"";
		$where	.=   !empty($end_date) ?" and date <='$end_date'":"";
		$where	.=   !empty($dtype) ?" and dtype ='$dtype'":"";

		$sql	=	"select sum(num) as num,uid from cfd_cfd_trade_log ".$where ." group by uid order by num desc";
		$query	=	$this->_db->query($sql);
		$result	=	$query->result_array();
		if (!empty($result)) {
			return $result;
		}
		return	0;
    }

	//交易用户
	public function getUserTrade($start_date="",$end_date="",$name="",$dtype=""){
		$where	=	"where uid not in(".$this->robot_str.")";
		$where	.=   !empty($name) ?" and name ='$name'":"";
		$where	.=   !empty($start_date) ?" and date >='$start_date'":"";
		$where	.=   !empty($end_date) ?" and date <='$end_date'":"";
		$where	.=   !empty($dtype) ?" and dtype ='$dtype'":"";

		$sql	=	"select uid from cfd_cfd_trade_log ".$where ." group by uid";
		$query	=	$this->_db->query($sql);
		$result	=	$query->result_array();
		if (!empty($result)) {
			return $result;
		}
		return	0;
    }

	//上线人数
	public function getInviteNum($start_date="",$end_date=""){
		$where	=	"where 1=1";
		$where	.=   !empty($start_date) ?" and create_time >='$start_date'":"";
		$where	.=   !empty($end_date) ?" and create_time <='$end_date'":"";

		$sql	=	"select count(DISTINCT master) num from cfd_invite ".$where;
		$query	=	$this->_db->query($sql);
		$row	=	$query->row_array();
		if (!empty($row['num'])) {
			return $row['num'];
		}
		return	0;
    }

	public function getCfdFee($uid="",$start_date="",$end_date="",$name=""){
		$where	=	"where pre_open_comm >0";
		$where	.=   !empty($uid) ?" and uid ='$uid'":" and uid not in(".$this->robot_str.")";
		$where	.=   !empty($name) ?" and name ='$name'":"";
		$where	.=   !empty($start_date) ?" and date >='$start_date'":"";
		$where	.=   !empty($end_date) ?" and date <='$end_date'":"";

		$sql	=	"SELECT sum(pre_open_comm*num) as fee from cfd_cfd_trade_log ".$where;
		$query	=	$this->_db->query($sql);
		$row	=	$query->row_array();
		if (!empty($row['fee'])) {
			return del0($row['fee']);
		}
		return	0;
    }

	public function getUserNum($start_date="",$end_date=""){
		$where	=	"where 1=1";
		$where	.=   !empty($start_date) ?" and create_time >='$start_date'":"";
		$where	.=   !empty($end_date) ?" and create_time <='$end_date'":"";

		$sql	=	"SELECT count(*) as num from cfd_user_info ".$where;
		$query	=	$this->_db->query($sql);
		$row	=	$query->row_array();
		if (!empty($row['num'])) {
			return $row['num'];
		}
		return	0;
    }

	public function getCnyBalance($uid=""){
		$where	=	"where cny >0";
		$where	.=   !empty($uid) ?" and uid ='$uid'":" and uid not in(".$this->robot_str.")";

		$sql	=	"SELECT sum(cny) as total_cny from cfd_user_info ".$where;
		$query	=	$this->_db->query($sql);
		$row	=	$query->row_array();
		if (!empty($row['total_cny'])) {
			return del0($row['total_cny']);
		}
		return	0;
    }

	public function getCfdInvite($uid=""){
		$where	=	"where 1 = 1";
		$where	.=   !empty($uid) ?" and master ='$uid'":"";

		$sql	=	"SELECT sum(cny) as invite from cfd_invite ".$where;
		$query	=	$this->_db->query($sql);
		$row	=	$query->row_array();
		if (!empty($row['invite'])) {
			return del0($row['invite']);
		}
		return	0;
    }

	public function getCfdInviteByDetails($uid="",$start_date="",$end_date=""){
		$where	=	"where 1 = 1";
		$where	.=   !empty($uid) ?" and master ='$uid'":"";
		$where	.=   !empty($start_date) ?" and create_time >='$start_date'":"";
		$where	.=   !empty($end_date) ?" and create_time <='$end_date'":"";

		$sql	=	"SELECT sum(cny) as invite from cfd_invite_details ".$where;
		$query	=	$this->_db->query($sql);
		$row	=	$query->row_array();
		if (!empty($row['invite'])) {
			return del0($row['invite']);
		}
		return	0;
    }

    public function getCfdChiInfo($uid=""){
		//加载CFD产品model
		$this->ci->load->model('cfd_model');
		//加载CFD操作类
		$this->ci->load->library('cfdClass');
		
		//加载CFD配置
		$this->ci->load->config('cfd');

		$where['flag']	=	0;
		if(!empty($uid)){
			$where['uid']	=	$uid;
		}
		$positionList = $this->ci->cfd_model->getPosition($where,'all','id DESC');

		$chi_data	=	array();
		$robot_arr	=	explode(",",$this->robot_str);
		if (!empty($positionList)) foreach($positionList as $position){
			if(!$this->ci->cfdclass->setProduct($position['name'],'IB')){//设置产品
				continue;
			}
			if(!$this->ci->cfdclass->setDenom($position['dtype'])){//设置计价方式
				continue;
			}
			
			if(in_array($position['uid'],$robot_arr)){
				continue;
			}

			$redisData		= $this->ci->cfdclass->redisAllData();
			$lastest_price	= isset($redisData['trade']['LAST_PRICE']) ? $redisData['trade']['LAST_PRICE'] : 0;
			$position['total'] = bcadd($position['num'],$position['block_num']);
			$position_yk	= del0($this->ci->cfdclass->countYK($lastest_price,$position['price'],$position['total'],$position['bs'],$position['commission'],$position['overnight']));

			if($position_yk < 0 && $position['dtype']=='ticket'){
				$position_yk	=	0;
			}
			
			if(!isset($chi_data[$position['name']]['num'][$position['bs']])){
				$chi_data[$position['name']]['num'][$position['bs']]	=	0;
			}

			if(!isset($chi_data[$position['name']]['value'][$position['bs']])){
				$chi_data[$position['name']]['value'][$position['bs']]	=	0;
			}

			if(!isset($chi_data[$position['name']]['dtype'][$position['dtype']])){
				$chi_data[$position['name']]['dtype'][$position['dtype']]	=	0;
			}

			$chi_data[$position['name']]['num'][$position['bs']]		=	bcadd($position['num'],$chi_data[$position['name']]['num'][$position['bs']]);
			$chi_data[$position['name']]['value'][$position['bs']]		+=	bcmul($position['num'],10,2);
			$chi_data[$position['name']]['dtype'][$position['dtype']]	+=	bcmul($position['num'],10,2);
			$chi_data[$position['name']][$position['dtype'].'_yk']		=	isset($chi_data[$position['name']][$position['dtype'].'_yk'])?bcadd($position_yk,$chi_data[$position['name']][$position['dtype'].'_yk'],2):$position_yk;
			$chi_data[$position['name']]['yk']		=	isset($chi_data[$position['name']]['yk'])?bcadd($position_yk,$chi_data[$position['name']]['yk'],2):$position_yk;
		}

		return $chi_data;
    }

	public function getTotalWithdraw($where="1=1"){
		$sql	=	"select sum(amount) as total_amount  from cfd_cny_withdraw where send_status !='-1' and ".$where;
		$query	=	$this->_db->query($sql);
		$row	=	$query->row_array();
		if (!empty($row['total_amount'])) {
			return del0($row['total_amount']);
		}
		return 0;
	}

	public function getTotalDeposit($where="1=1"){
		$sql	=	"select sum(send_amount) as total_amount from cfd_cny_deposit where pay_status =1 and ".$where;
		$query	=	$this->_db->query($sql);
		$row	=	$query->row_array();
		if (!empty($row['total_amount'])) {
			return del0($row['total_amount']);
		}
		return 0;
	}

	public function getTotalDepositNum($where="1=1"){
		$sql	=	"select count(distinct uid) as num from cfd_cny_deposit where pay_status =1 and ".$where;
		$query	=	$this->_db->query($sql);
		$row	=	$query->row_array();
		if (!empty($row['num'])) {
			return $row['num'];
		}
		return 0;
	}

	public function getOnlineNum($where="1=1"){
		$sql	=	"select count(*) total from cfd_online where ".$where;
		$query	=	$this->_db->query($sql);
		$row	=	$query->row_array();
		if (!empty($row['total'])) {
			return $row['total'];
		}
		return 0;
	}
}